﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Collections;
namespace GPMS.BLL
{
    public static class ExcelToSQL
    {


        /// <summary>
        /// 传入Excel的路径，处理之后返回一个datatable
        /// </summary>
        /// <param name="excelPath">Excel路径</param>
        /// <returns>DataTable</returns>
        public static DataTable ExcelToDataTable(string excelPath)  //把Excel里的数据转换为DataTable，并返回DataTable  
        {
            string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelPath + ";Extended Properties='Excel 8.0;IMEX=1'";
            System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon);
            string strCom = "SELECT * FROM [Sheet1$]";
            DataTable dt;
            try
            {
                Conn.Open();
                System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
                DataSet ds = new DataSet();
                myCommand.Fill(ds, "[Sheet1$]");
                Conn.Close();
                dt = ds.Tables[0];
            }
            catch (Exception)
            {
                return null;
            }
            return dt;
        }

    }
}